package com.mtt.erp.file.controller;

import com.mtt.erp.common.api.R;
import com.mtt.erp.common.api.ResultCode;
import com.mtt.erp.common.exception.MttErpException;
import com.mtt.erp.file.domain.GoodsFileInfo;
import com.mtt.erp.file.dto.ImageDto;
import com.mtt.erp.file.service.GoodsFileInfoService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.validation.constraints.NotBlank;
import java.util.*;

/**
 * @Auther: SJZ    2907374717@qq.com
 * @Date: 2019/5/28 19:58
 * @Description: 文件相关
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Api(value = "上传文件", tags = "M010-文件-文件管理相关接口")
public class FileController{

    @Autowired
    GoodsFileInfoService goodsFileInfoService;

    /**
     * 上传文件到腾讯云服务器
     *
     * @param file
     * @return
     * @throws MttErpException
     */
    @PostMapping(value="uploadQCloud",headers = "content-type=multipart/form-data")
    @ApiOperation(value = "文件上传", notes = "文件上传API,文件上传到腾讯云服务器", position = 1)
    public R uploadQCloud(
            @ApiParam(name="file",value="文件")
            @RequestParam(value="file", required = true) MultipartFile file){
        try {
            Boolean updateResult = goodsFileInfoService.uploadQCloud(file);
            if(updateResult){
                // 上传成功
                return R.success(ResultCode.SUCCESS);
            } else {
                return R.fail(ResultCode.FAILURE);
            }
        } catch (MttErpException e) {
            // service抛出异常;判断异常的编码是否为图片已存在,如不是则是文件上传异常
            Integer code = ResultCode.FILE_EXISTS_ERROR.getCode();
            if (e.getMessage().equals(code.toString())) {
                // 图片已存在
                return R.fail(ResultCode.FILE_EXISTS_ERROR);
            } else {
                // 上传异常
                return R.fail(ResultCode.FILE_UPLOAD_ERROR);
            }
        }
    }

    /**
     * 根据id查询指定文件
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询文件",notes = "根据id查询文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "path")
    })
    public R<GoodsFileInfo> findFile(@NotBlank(message = "required") @PathVariable String id){
        GoodsFileInfo goodsFileInfo = this.goodsFileInfoService.getById(id);
        if (goodsFileInfo == null) {
            return R.fail(ResultCode.NO_CONTENT);
        } else {
           return R.data(goodsFileInfo);
        }
    }

    /**
     * 根据idStr查询文件列表
     * @param idStr
     * @return
     */
    @GetMapping("findFileListByIds")
    @ApiOperation(value = "查询文件",notes = "根据id串查询文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "idStr",value = "id串,以逗号分割",paramType = "query")
    })
    public R<List<ImageDto>> findFileListByIds(@NotBlank(message = "required") String idStr){
        List<String> ids = null;
        if (idStr != null && !"".equals(idStr)){
            ids = Arrays.asList(idStr.split(","));
        }
        List<ImageDto> list= goodsFileInfoService.findImageList(ids);
        if (list == null || list.size() < 1) {
            return R.fail(ResultCode.NO_CONTENT);
        } else {
            return R.data(list);
        }
    }

    /**
     * 根据fileKeyStr查询文件列表
     * @param fileKeyStr
     * @return
     */
    @GetMapping("findFileListByKeys")
    @ApiOperation(value = "查询文件",notes = "根据fileKey串查询文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileKeyStr",value = "fileKey串,以逗号分割",paramType = "query")
    })
    public R<List<ImageDto>> findFileListByKeys(@NotBlank(message = "required") String fileKeyStr){
        List<String> fileKeys=null;
        if (fileKeyStr != null && !"".equals(fileKeyStr)){
            fileKeys = Arrays.asList(fileKeyStr.split(","));
        }
        List<ImageDto> list= goodsFileInfoService.findImageListByIkeyS(fileKeys);
        if (list == null || list.size() < 1) {
            return R.fail(ResultCode.NO_CONTENT);
        } else {
            return R.data(list);
        }
    }

    /**
     *  根据fileKey删除文件
     * @param fileKeys
     * @return
     */
    @DeleteMapping
    @ApiOperation(value = "删除文件",notes = "批量删除,根据fileKey串删除文件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileKeys",value = "fileKeys,fileKey串以逗号分隔",required = true,paramType = "query")
    })
    public R deleteFile(@NotBlank(message = "required")  String fileKeys){
        try {
            Boolean result = goodsFileInfoService.deleteFile(fileKeys);
            if(result){
                return R.success(ResultCode.SUCCESS);
            }else{
                return R.fail(ResultCode.FAILURE);
            }
        } catch (MttErpException e) {
            return R.fail(ResultCode.FAILURE);
        }
    }
}
